package com.hsnn.medstgmini.pro.project.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.model.form.ProdBidCatalogForm;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.pro.project.service.PsBidCatalogManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 配送企业端招标目录管理
 * @author wangbing
 * @date 2015年8月24日
 */
@Controller
@RequestMapping("/psBidCatalog")
public class PsBidCatalogController {

	private static final Logger logger = LoggerFactory
			.getLogger(PsBidCatalogController.class);
	
	@Autowired
	private PsBidCatalogManager psBidCatalogManager;
	
	@Autowired
	private BidProjectManager bidProjectManager;
	
	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;

	/**
	 * @category 获取当前登陆人信息
	 * @author 言科
	 * @date 2015年7月22日 SysUser
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}

	/**
	 * @category 跳转招标目录页面
	 * @date 2015年8月25
	 * @return
	 */
	@RequestMapping("/toBidCatalogList")
	public String toBidCatalogList(RedirectAttributes attr) {
		logger.info("enter toBidCatalogList method!");
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		String defalutProjId=  user.getDefaultProjId();
		if ( StringUtils.isNotBlank(defalutProjId) ) {
			BidProject bp=  bidProjectManager.getBidProjectById(defalutProjId);
			 attr.addAttribute("projId",defalutProjId);  
			 attr.addAttribute("name",bp.getName()); 
		     return "redirect:/psBidCatalog/toHaveAddDrugQualityList.html";
		}
		return "/pro/project/bidCatalogListByPs";
	}

	/**
	 * @category 获取招标目录列表
	 * @date 2015年8月25日
	 * @param form
	 * @return
	 */
	@RequestMapping("/getBidCatalogList")
	@ResponseBody
	public Pagination getBidCatalogList(ProdBidCatalogForm form) {
		logger.info("enter getBidCatalogList method!");
		Pagination page = new Pagination(request);
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		try {
			String defalutProjId=  user.getDefaultProjId();
			if (StringUtils.isNotBlank(defalutProjId)) {
				page.getConditions().put("defalutProjId", defalutProjId);
				form.setProjId(defalutProjId);
			}
			page.setForm(form);
			psBidCatalogManager.getBidCatalogList(page);
			page.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 查询招标目录的项目列表
	 * @author 应晓川
	 * @date 2015年9月28日10:45:12
	 * @return
	 */
	@RequestMapping("/getDataList")
	@ResponseBody
	public Pagination getDataList() {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		StdCompinfo sc = (StdCompinfo)user.getInfo();
		try {
		if(StringUtils.isNotBlank(sc.getCompId())){
			page.setForm(sc);
		}
		if (StringUtils.isNotBlank(user.getDefaultProjId())) {//已设置默认项目
			page.getConditions().put("projId", user.getDefaultProjId());
			page.setForm(user.getDefaultProjId());
		}else{
			if (StringUtils.isNotBlank(request.getParameter("projId"))) {
				page.getConditions().put("projId", request.getParameter("projId"));
			}
		}
		psBidCatalogManager.getDataList(page);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * 
	 *@category 跳向已添加品规数
	 *@author 徐国鹏
	 *@date 2015年8月19日 上午10:38:13
	 *@return 
	 */
	@RequestMapping("/toHaveAddDrugQualityList")
	public String toHaveAddDrugQualityList(){
		request.setAttribute("projId", request.getParameter("projId"));
		String name = request.getParameter("name");
		try {
			if (StringUtils.isNotBlank(name)){
				name = new String(name.getBytes("ISO8859-1"),"utf-8");
			}
			request.setAttribute("name", name);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
		}
		return "/pro/project/bidHaveAddDrugQualityListByComp";
	}
	
	
}
